var Operator = function() {

    var tableId = 'data-table';

    // 筛选
    /*var tableSearch = function (params) {
        $('#' + tableId).DataTable().search(JSON.stringify(param)).draw();
    };*/

    return {
        init: function() {
            $(document).keydown(function(event){
                switch(event.keyCode){
                    case 13:return false;
                }
            });
        }

    };

}();

jQuery(document).ready(function() {
    Operator.init();


    var tableId = 'data-table';

    // 添加
    $('.add-btn').on('click', function(){
        /*
        var url = $(this).data('href');
        var title = $(this).attr('data-title');
        var option = {
            area: ['1000px', '500px']
        };
        open_iframe_layer(url, title, option);
        */
        /*
        var title = $(this).attr('data-title');
        var content = 'Demo Content';
        var options = {
            'width' : '1000px',
            'height' : '500px',
        };
        open_modal(title, content, options);
        */


        var title = $(this).attr('data-title');
        var content = $('#modal-content').html();
        /*var content = '';
        content += '<div class="row js-check-wrap">';
        content += '<div class="col-xs-12">';
        content += '<div class="box box-success">';
            content += '<div class="box-header with-border">';
                content += '<div class="row">';
                content += '<div class="col-md-9">';
                content += '<form action="" class="form" method="post">';
                content += '<div class="form-inline">';
                content += '<label>姓名 ：</label>';
                content += '<input type="text" class="form-control" name="name" value="">';
                content += '&nbsp;&nbsp;&nbsp;';
                content += '<button type="submit" class="btn btn-success" title="筛选"><i class="fa fa-search"></i> 筛选</button>';
                content += '</div>';
                content += '</form>';
                content += '</div>';
                content += '</div>';
            content += '</div>';
            content += '<div class="box-body">';
                content += '<form action="" class="js-ajax-form post-form" method="post">';
                content += '<div class="table-responsive">';
                content += '<table id="data-table" class="table table-bordered table-hover dataTable">';
                content += '<thead>';
                content += '<th><input type="checkbox" class="js-check-all" data-direction="x" data-checklist="js-check-x"></label></th>';
                content += '<th>头像</th>';
                content += '<th>姓名</th>';
                content += '<th>性别</th>';
                content += '<th>出生年月</th>';
                content += '<th>身份证号</th>';
                content += '<th>手机号</th>';
                content += '<th>名族</th>';
                content += '<th>文化程度</th>';
                content += '<th>健康状况</th>';
                content += '</thead>';
                content += '</table>';
                content += '</div>';
                content += '</form>';
            content += '</div>';
        content += '</div>';
        content += '</div>';
        content += '</div>';*/

        var options = {
            'width' : '1000px',
        };
        open_modal(title, content, options);



        $(document).find('#' + tableId).dataTable({
            "processing": true, // 开启服务器模式
            "ordering": false, // 禁止排序
            serverSide: true,
            "ajax": {
                url: $('.member_list').val(),
            },
            "autoWidth": false,
            "columns": [
                {"width": "5%", "data": "id", "render":
                    function (data, type, row, meta) {
                        return '<input type="checkbox" class="js-check" name="ids[]" value="'+data+'" data-yid="js-check-y" data-xid="js-check-x">';
                    }
                },
                {"data": "avatar", "render":
                    function (data, type, row, meta) {
                        return '<img src="'+data+'" style="width: 40px;height: 40px;">';
                    }
                },
                {"data": "name"},
                {"data": "gender"},
                {"data": "birthday"},
                {"data": "identityNumber", "render":
                    function (data, type, row, meta) {
                        //return data;
                        return data + '<input type="checkbox" class="js-check" name="identityNumber[]" value="'+data+'" style="display: none;">';
                    }
                },
                {"data": "mobile"},
                /*{"data": "ethnicId"},
                {"data": "education"},
                {"data": "health"},*/
            ],
            "language":{
                "lengthMenu": "每页显示 _MENU_ 条记录",
                "emptyTable": "暂无数据记录",
                "info": "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords": "没有搜索到匹配记录",
                "infoEmpty": "",
                "infoFiltered": "",
                "processing": "数据加载中,请稍后...",
                "paginate": {
                    "first": "首页",
                    "previous": "",
                    "next": "",
                    "last": "末页"
                }
            },
            "pageLength": 10,
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
            "<t>" +
            "<'row table-footer'<'col-md-5'i><'col-md-7'p>>",
            "fnCreatedRow": function(nRow, aData, iDataIndex){
                var curentId = $("input[name='id']").val();
                if(curentId == aData['id']) {
                    //
                }
            },
            "fnInitComplete": function() {}
        });

        tableSearch();
    });

    // 设置
    $('.set-btn').on('click', function(){
        var url = $(this).data('href');
        var title = $(this).attr('data-title');
        var option = {
            area: ['600px', '500px']
        };

        open_iframe_layer(url, title, option);
    });

    // checkbox 全选
    $(document).on('click', '.js-check-all', function () {
        if ($(this).prop('checked')) {
            //全选状态
            $(document).find('.js-check').prop('checked', true);
        } else {
            //非全选状态
            $(document).find('.js-check').prop('checked', false);
        }
    });
    $(document).on('click', '.js-check', function () {
        if ($(this).prop('checked')) {
            if ($(document).find('.js-check').filter(':checked').length === $(document).find('.js-check').length) {
                // 已选择和未选择的复选框数相等
                $(document).find('.js-check-all').prop('checked', true);
            }
            $(this).parents('tr').find('.js-check').prop('checked', true);
        } else {
            $(document).find('.js-check-all').prop('checked', false);
            $(this).parents('tr').find('.js-check').prop('checked', false);
        }
    });


    // 筛选
    $(document).on('click', '.filter-btn', function(event){
        var name = $("#searchForm input[name='name']").val();
        var params = {"name" : ['%' + name + '%', 'like']};

        tableSearch(params);
    });

    $(document).on('click', '.confirm-btn', function(){
        var url = $(document).find('.add-post-form').attr('action');

        var data = $(document).find('.add-post-form').serialize();

        if($(document).find('.js-check').filter(':checked').length <= 0){
            layer.msg('请至少选择一条数据');
            return false;
        }

        $.ajax({
            url: url,
            type: 'POST',
            data: data,
            dataType : 'json',
            success: function (data) {
                if(data.status == 1){
                    $('#common-modal').modal('hide');
                    window.location.reload();
                }else{
                    layer.msg(data.msg);
                }
            }
        });
        return false;
    });


    function tableSearch(options) {
        var ids = '';
        $('.list-form tbody').find('.member_id').each(function(){
            ids += ',' + $(this).val();
        });
        var params = {
            'id': [ids.substr(1), 'not in'],
        };
        params = options ? $.extend(params, options) : params;

        $('#' + tableId).DataTable().search(JSON.stringify(params)).draw();
    }

});